package com.cskaoyan.service;

import com.cskaoyan.bean.*;
import com.cskaoyan.bean.common.BaseRespVo;
import com.cskaoyan.bean.common.CommonData;
import com.cskaoyan.mapper.MarketCategoryMapper;
import com.cskaoyan.mapper.MarketLogMapper;
import com.cskaoyan.util.AuthenticationUtil;
import com.cskaoyan.util.MyBatisUtil;
import org.apache.ibatis.session.SqlSession;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class CategoryServiceImpl implements CategoryService {
    @Override
    public MarketCategory findById(Integer id) {
        SqlSession sqlSession = MyBatisUtil.getSqlSession();
        MarketCategoryMapper mapper = sqlSession.getMapper(MarketCategoryMapper.class);
        MarketCategory marketCategory = mapper.selectByPrimaryKey(id);
        sqlSession.close();
        return marketCategory;
    }
    @Override
    public BaseRespVo list() {

        SqlSession sqlSession =MyBatisUtil.getSqlSession();
        MarketCategoryMapper categoryMapper = sqlSession.getMapper(MarketCategoryMapper.class);
        MarketCategoryExample marketCategoryExample = new MarketCategoryExample();
        marketCategoryExample.createCriteria().andPidEqualTo(0);
        //获得一级目录
        List<MarketCategory> marketCategories = categoryMapper.selectByExample(marketCategoryExample);


        for (MarketCategory marketCategory : marketCategories) {
            marketCategoryExample.clear();
            marketCategoryExample.createCriteria().andPidEqualTo(marketCategory.getId());
            List<MarketCategory> marketCategories2 = categoryMapper.selectByExample(marketCategoryExample);
            marketCategory.setChildren(marketCategories2);
        }

        CommonData data = CommonData.data(marketCategories);


        return BaseRespVo.ok(data);
    }

    @Override
    public BaseRespVo update(MarketCategory marketCategory) {
        SqlSession sqlSession = MyBatisUtil.getSqlSession();
        MarketCategoryMapper categoryMapper = sqlSession.getMapper(MarketCategoryMapper.class);
        categoryMapper.updateByPrimaryKey(marketCategory);

        sqlSession.commit();
        sqlSession.close();
        return BaseRespVo.ok();
    }

    @Override
    public BaseRespVo create(MarketCategory marketCategory) {
        SqlSession sqlSession = MyBatisUtil.getSqlSession();
        MarketCategoryMapper categoryMapper = sqlSession.getMapper(MarketCategoryMapper.class);
        int insert = categoryMapper.insert(marketCategory);


        //日志修改

        MarketAdmin marketAdmin = (MarketAdmin) AuthenticationUtil.getPrincipal();
        String username = marketAdmin.getUsername();
        MarketLogMapper marketLogMapper = sqlSession.getMapper(MarketLogMapper.class);
        MarketLog marketLog = new MarketLog();

        marketLog.setAdmin(username);
        marketLog.setAction("创建商品");
        marketLog.setAddTime(new Date());
        marketLog.setUpdateTime(new Date());
//        marketLog.setIp(ip);
        marketLog.setType(3);

        marketLogMapper.insert(marketLog);


        sqlSession.commit();
        sqlSession.close();
        return BaseRespVo.ok(marketCategory);
    }

    @Override
    public BaseRespVo l1() {
        SqlSession sqlSession =MyBatisUtil.getSqlSession();
        MarketCategoryMapper categoryMapper = sqlSession.getMapper(MarketCategoryMapper.class);
        MarketCategoryExample marketCategoryExample = new MarketCategoryExample();
        marketCategoryExample.createCriteria().andPidEqualTo(0);
        //获得一级目录
        List<MarketCategory> marketCategories = categoryMapper.selectByExample(marketCategoryExample);
        ArrayList<CommonLable> categoryLables = new ArrayList<>();
        for (MarketCategory marketCategory : marketCategories) {
            categoryLables.add(new CommonLable(marketCategory.getId(),marketCategory.getName()));
        }

        CommonData data = CommonData.data(categoryLables);
        return BaseRespVo.ok(data);
    }

    @Override
    public BaseRespVo delete(MarketCategory marketCategory) {
        SqlSession sqlSession =MyBatisUtil.getSqlSession();
        MarketCategoryMapper categoryMapper = sqlSession.getMapper(MarketCategoryMapper.class);
        categoryMapper.deleteByPrimaryKey(marketCategory.getId());
        sqlSession.commit();
        sqlSession.close();
        return BaseRespVo.ok();
    }
}

